﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Web.UI;
using TA.Turbo;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Base class for all entity list controls.
	/// </summary>
	/// <typeparam name="T">The entity type.</typeparam>
	/// <typeparam name="C">The type of list control.</typeparam>
	public abstract class BaseEntityMultiValueListControl<T, C> : BaseEntityControl<T, C>
		where C : BaseValueControl<string>, IMultiValueListControl, new()
	{
		#region PUBLIC
		/// <summary>
		/// Ctor.
		/// </summary>
		public BaseEntityMultiValueListControl()
		{

		}
		#region PROPS
		/// <summary>
		/// Returns a comma (',') separated list of text values of all selected items of this list control, or empty string if no items are selected. Use the same format (',' - separated list) to set selected items by their text values.
		/// </summary>
		public override string Text
		{
			get
			{
				StringBuilder sb = new StringBuilder();
				foreach (ListItem li in this.Child.SelectedItems)
				{
					if (sb.Length > 0)
						sb.Append(",");
					sb.Append(li.Text);
				}
				return sb.ToString();
			}
			set
			{
				if (value == null)
					Child.Value = null;
				else
				{
					string[] texts = value.Split(new[] { ',' });
					Child.SelectedItems.Clear();
					foreach (string text in texts)
					{
						Child.SelectedItems.Add(Child.Items.FindByText(text));
					}
				}
			}
		}
		/// <summary>
		/// Default text - used to set the list control's FirstItemText.
		/// </summary>
		public override string DefaultText
		{
			get
			{
				return base.DefaultText;
			}
			set
			{
				base.DefaultText = value;
				//if (Child != null)
				//  Child.FirstItemText = value;
			}
		}
		/// <summary>
		/// When implemented, gets or sets range of values of type T.
		/// </summary>
		public abstract IEnumerable<T> Values { get; set; }
		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		#endregion
		#region METHODS
		/// <summary>
		/// Assigns DefaultText to Child.FirstItemText.
		/// </summary>
		protected override void CreateChild()
		{
			base.CreateChild();

			//Child.FirstItemText = this.DefaultText;
		}
		#endregion
		#endregion
	}
}
